package com.healthcloud.controller.modules.patient;

import com.healthcloud.entity.questionnaire.*;
import com.healthcloud.service.questionnaire.QuestionnaireService;
import com.healthcloud.utils.ApiRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;

/**
 * 问卷调查
 * 
 * @author zj
 */
@ApiIgnore
@Controller
@RequestMapping("questionnaire")
public class QuestionnaireAddDataController {

    @Autowired
    private QuestionnaireService questionnaireService;

    /**
     * 以下都是添加数据
     */
    @ApiIgnore
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "bus/questionnaire/t_index";
    }

    @ApiIgnore
    @RequestMapping(value = "/toAddTitle", method = RequestMethod.GET)
    public String toAddTitle(Model model, Integer type) {
        model.addAttribute("type", type);
        return "bus/questionnaire/t_title";
    }

    @ApiIgnore
    @RequestMapping(value = "/toAddSubtitle", method = RequestMethod.GET)
    public String toAddSubtitle(Model model, Integer titleId) {
        model.addAttribute("titleId", titleId);
        return "bus/questionnaire/t_subtitle";
    }

    @ApiIgnore
    @RequestMapping(value = "/toAddQuestion", method = RequestMethod.GET)
    public String toAddQuestion(Model model, Integer subtitleId) {
        model.addAttribute("subtitleId", subtitleId);
        return "bus/questionnaire/t_question";
    }

    @ApiIgnore
    @RequestMapping(value = "/toAddOption", method = RequestMethod.GET)
    public String toAddOption(Model model, Integer questionId) {
        model.addAttribute("questionId", questionId);
        return "bus/questionnaire/t_option";
    }

    @RequestMapping(value = "/addTitle", method = RequestMethod.GET)
    public String addTitle(Model model, QuestionnaireTitleEntity title){
        model.addAttribute("type", title.getType());
        questionnaireService.saveTitle(title);
        return "bus/questionnaire/t_title";
    }

    @RequestMapping(value = "/addSubtitle", method = RequestMethod.GET)
    public String addSubtitle(Model model, QuestionnaireSubtitleEntity subtitle){
        model.addAttribute("titleId", subtitle.getTitleId());
        questionnaireService.saveSubtitle(subtitle);
        return "bus/questionnaire/t_subtitle";
    }

    @RequestMapping(value = "/addQuestion", method = RequestMethod.GET)
    public String addQuestion(Model model, QuestionnaireQuestionEntity question){
        model.addAttribute("subtitleId", question.getSubtitleId());
        model.addAttribute("subtitleId", question.getSubtitleId());
        questionnaireService.saveQuestion(question);
        return "bus/questionnaire/t_question";
    }

    @RequestMapping(value = "/addOption", method = RequestMethod.GET)
    public String addOption(Model model, QuestionnaireOptionEntity option){
        model.addAttribute("questionId", option.getQuestionId());
        questionnaireService.saveOption(option);
        return "bus/questionnaire/t_option";
    }

    @ApiIgnore
    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public ApiRes toAddOption(String name, Long type, Long titleId, Long subtitleId, Long questionId) {
        List list = new ArrayList();
        if ("title".equals(name)){
            QuestionnaireTitleEntity title = new QuestionnaireTitleEntity();
            title.setType(type);
            List<QuestionnaireTitleEntity> ts = questionnaireService.queryListTitle(title, null);
            if (ts!=null&&ts.size()>0){
                for (QuestionnaireTitleEntity e:
                        ts) {
                    Map<String, Object> map = new TreeMap<>();
                    map.put("text", e.getTitle());
                    map.put("value", e.getId());
                    list.add(map);
                }
            }
        }else if ("subtitle".equals(name)){
            QuestionnaireSubtitleEntity subtitle = new QuestionnaireSubtitleEntity();
            subtitle.setTitleId(titleId);
            List<QuestionnaireSubtitleEntity> ss = questionnaireService.queryListSubtitle(subtitle, null);
            if (ss!=null&&ss.size()>0){
                for (QuestionnaireSubtitleEntity s:
                        ss) {
                    Map<String, Object> map = new TreeMap<>();
                    map.put("text", s.getSubtitle());
                    map.put("value", s.getId());
                    list.add(map);
                }
            }
        }else if ("question".equals(name)){
            QuestionnaireQuestionEntity question = new QuestionnaireQuestionEntity();
            question.setSubtitleId(subtitleId);
            List<QuestionnaireQuestionVo> qs = questionnaireService.queryListQuestionVo(question, null);
            if (qs!=null&&qs.size()>0){
                for (QuestionnaireQuestionEntity q:
                        qs) {
                    Map<String, Object> map = new TreeMap<>();
                    map.put("text", q.getQuestion());
                    map.put("value", q.getId());
                    list.add(map);
                }
            }
        }else if ("option".equals(name)){
            QuestionnaireOptionEntity option = new QuestionnaireOptionEntity();
            option.setQuestionId(questionId);
            List<QuestionnaireOptionEntity> os = questionnaireService.queryListOption(option, null);
            if (os!=null&&os.size()>0){
                for (QuestionnaireOptionEntity o:
                        os) {
                    Map<String, Object> map = new TreeMap<>();
                    map.put("text", o.getText());
                    map.put("value", o.getId());
                    list.add(map);
                }
            }
        }
        return new ApiRes(list);
    }

}
